package com.lc.japaneseapp;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;

import com.example.japaneseapp.R;
import com.lc.DetailCharacter.DetailGridView;
import com.lc.SlidingPaneUnit.WordFragment;
import com.lc.adapter.CharacterAdapter;

public class ViewCharacterActivity extends Activity {

	private CharacterAdapter adapterCharacter;
	private GridView gridviewOfCharacter;
	private DetailGridView detailCharacter;
	private Button btnChangGridTypeChar, btnTest,btnTesttv, btnNewWord;
	private boolean isHiragana = true;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.main_layout);
		btnChangGridTypeChar = (Button) findViewById(R.id.btnChangeTypeChar);
		btnTest = (Button) findViewById(R.id.btnTest);
		btnTesttv = (Button) findViewById(R.id.btnTest1);
		btnChangGridTypeChar.setText("あ");
		btnNewWord = (Button) findViewById(R.id.btnNewWord);
		// make and set a adapter to grid view
		adapterCharacter = new CharacterAdapter(this);
		adapterCharacter.setCharacterApdapter(isHiragana);
		isHiragana = !isHiragana;
		gridviewOfCharacter = (GridView) findViewById(R.id.characterHiraganaGrid);
		gridviewOfCharacter.setAdapter(adapterCharacter);
		gridviewOfCharacter
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {

						detailCharacter = new DetailGridView(
								ViewCharacterActivity.this);
						detailCharacter.showDetail(position, !isHiragana);

					}
				});
		btnChangGridTypeChar.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (isHiragana) {
					btnChangGridTypeChar.setText("あ");
				} else {
					btnChangGridTypeChar.setText("ア");
				}
				adapterCharacter.setCharacterApdapter(isHiragana);
				gridviewOfCharacter = (GridView) findViewById(R.id.characterHiraganaGrid);
				gridviewOfCharacter.setAdapter(adapterCharacter);

				isHiragana = !isHiragana;
			}
		});
		super.onCreate(savedInstanceState);

		btnTest.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ViewCharacterActivity.this,
						TestActivity.class);
				startActivity(intent);
			}
		});

		btnTesttv.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(ViewCharacterActivity.this,
						TestActivity.class);
				startActivity(intent);
			}
		});
		btnNewWord.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(ViewCharacterActivity.this,
						WordFragment.class);
				startActivity(intent);
				overridePendingTransition(R.anim.pull_in_left,
						R.anim.push_out_right);
			}
		});
		
	}
	private float x; float y;
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			x = event.getX();
			break;
		case MotionEvent.ACTION_UP:
			y = event.getX();
			float distance = x - y;
			if (Math.abs(distance) >300 && y < x){
				Intent intent = new Intent(ViewCharacterActivity.this,
						WordFragment.class);
				startActivityForResult(intent, 0);
				overridePendingTransition(R.anim.pull_in_left,
						R.anim.push_out_right);
			}
			break;
		}
		return super.onTouchEvent(event);
	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent ev) {
		   super.dispatchTouchEvent(ev);    
		    return this.onTouchEvent(ev); 
	}
}
